Skip to content

Conversation

musshorn
Copy link
Contributor

This pull implements the api to call the following:
https://wiki.libsdl.org/SDL3/SDL_GetJoystickPowerInfo
https://wiki.libsdl.org/SDL3/SDL_GetJoystickConnectionState
https://wiki.libsdl.org/SDL3/SDL_GetGamepadPowerInfo
https://wiki.libsdl.org/SDL3/SDL_GetGamepadConnectionState

It's worth noting though, the battery info returned from PowerInfo they have on the wiki:
You should never take a battery status as absolute truth. Batteries (especially failing batteries) are delicate hardware, and the values reported here are best estimates based on what that hardware reports. It's not uncommon for older batteries to lose stored power much faster than it reports, or completely drain when reporting it has 20 percent left, etc.

I found this in testing, I tried a wireless steam controller on battery and it did not report any battery percent. Other controllers may though. Even when using the SDL testcontroller it did not report anything.

Testing wired though with the following script works as expected

function love.draw()
    local sticks = love.joystick:getJoysticks()
    for _, stick in ipairs(sticks) do
        local name = stick:getName()
        local id = stick:getID()
        local battery = stick:getDeviceBatteryPercent()
        local power = stick:getDevicePowerState()
        local connection = stick:getDeviceConnectionState()

        love.graphics.print("name: " .. name, 100, 100)
        love.graphics.print("id: " .. id, 100, 120)
        love.graphics.print("battery power state: " .. power, 100, 140)
        love.graphics.print("battery percent: " .. battery, 100, 160)
        love.graphics.print("connection state: " .. connection, 100, 180)
    end
end

image

I suspect this somewhat resolves #2223

@musshorn
Copy link
Contributor Author

musshorn commented Oct 1, 2025

Thanks for the review @slime73 ! Appreciate it. I believe it's all resolved now.

@musshorn
Copy link
Contributor Author

Thanks, those were some obvious ones. I initialize the state now in both functions which makes it a bit simpler.

@slime73 slime73 merged commit c1e097a into love2d:main Oct 17, 2025
8 checks passed
@slime73
Copy link
Member

slime73 commented Oct 17, 2025

Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[REQUEST] Expose controller battery/power level via love.joystick (SDL_JoystickCurrentPowerLevel)

2 participants